package com.hoas.databox.core;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 简易数据寄存点
 */
public class EasyDataBox {

    /**
     * 命名空间
     */
    private String namespace;

    /**
     * 应用名称
     */
    private String appName;

    private DataRepostroy dataRepostroy;

    public EasyDataBox(String namespace, DataRepostroy dataRepostroy) {
        this.namespace = namespace;
        this.dataRepostroy = dataRepostroy;
    }


    public String deposit( String data, int second) {
        return deposit( data, null, second);
    }

    public String deposit( String data,String receiver, int time, TimeUnit timeUnit) {
        return deposit( data, receiver, (int) timeUnit.toSeconds( time ));
    }

    /**
     * 寄存
     *
     * @param data
     *      寄存信息
     * @param second
     *      寄存时间【秒】
     *
     * @return
     *      ticket
     */
    public String deposit( String data,String receiver, int second) {

        EasyData dataObj = new EasyData();
        dataObj.setNamespace( namespace );
        dataObj.setSender( getAppName() );
        dataObj.setRecever( receiver );
        dataObj.setData( data );
        dataObj.setTicket( genTicketNo() );

        boolean succ = dataRepostroy.save(dataObj, second);
        if (succ) {
            return dataObj.getTicket();
        }

        return null;
    }

    /**
     * 取件,取一次即失效
     * @param ticket
     *
     * @return
     *  data
     */
    public String takeout( String ticket ) {
        return takeout( ticket, null );
    }

    /**
     * 取件,取一次即失效
     * @param ticket 信息提取凭证
     * @param receiver 信息接收者
     *
     * @return
     *  data
     */
    public String takeout( String ticket, String receiver ) {

        EasyData boxData = dataRepostroy.query( namespace, ticket, false );
        if(boxData == null) {
            return null;
        }

        if (boxData.getRecever() == null || boxData.getRecever().length() == 0) {
            return boxData.getData();
        }

        if (boxData.getRecever().equalsIgnoreCase( receiver)) {
            return boxData.getData();
        }

        return null;
    }

    /**
     * 阅览，仅仅查看信息，但不去除，可下次继续阅览
     * @param ticket
     *
     * @return
     *  data
     */
    public String read( String ticket) {
        return read( ticket, null);
    }

    /**
     * 阅览，仅仅查看信息，但不去除，可下次继续阅览
     * @param ticket
     * @param receiver 信息接收者
     *
     * @return
     *  data
     */
    public String read( String ticket, String receiver ) {

        EasyData boxData = dataRepostroy.query( namespace, ticket, true );
        if(boxData == null) {
            return null;
        }
        if (boxData.getRecever() == null || boxData.getRecever().length() == 0) {
            return boxData.getData();
        }

        if (boxData.getRecever().equalsIgnoreCase( receiver)) {
            return boxData.getData();
        }

        return null;


    }

    protected String genTicketNo() {
        return UUID.randomUUID().toString();
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

}
